package com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.BankCards;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.GetBankAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.GetBankBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class SelectBankActivity extends BaseActivity {

    @Bind(R.id.finish_img)
    ImageButton mIb_back;
    @Bind(R.id.view_title)
    TextView mV_title;
    @Bind(R.id.lv_select_bank)
    ListView lv_select_bank;


    private GetBankBean getBankBean;
    private GetBankAdapter adapter;
    private List<GetBankBean.DataBean> datas1;
    private int code = 0;
    private Intent i;
    private String con;


    @Override
    public void init() {
        setStatuColor();

        con = getIntent().getStringExtra("context");

        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.chooseBankCard);


        datas1 = new ArrayList<>();
        //给ListView_main_wordsList注册上下文菜单
        registerForContextMenu(lv_select_bank);

        //银行
        final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
        reqequestManager.httpPost(Api.BaseUrl + Api.Get_Bank, Api.getGetBank(SharedPrefsUtil.getValue(SelectBankActivity.this, "wallet", "token", "")), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                getBankBean = gson.fromJson(Response, GetBankBean.class);
                code = getBankBean.getCode();
                if (code == 200) {
                    datas1.addAll(getBankBean.getData());
                    initAdapter();
                    OnClick();
                }else{
                    Toast.makeText(getApplicationContext(),getBankBean.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(String error) {
            }
        });


        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_select_bank;
    }
    //展示adapter
    private void initAdapter() {
        adapter = new GetBankAdapter(this, datas1);
        lv_select_bank.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
    //点击adapter时
    private void OnClick() {
        adapter.getClickPst(new GetBankAdapter.OnClick() {
            @Override
            public void Click(GetBankBean.DataBean dataBean) {
                if ("NewBankCardsActivity".equals(con)){
                    i = new Intent(getApplicationContext(), NewBankCardsActivity.class);
                    i.putExtra("tv_bank_open",dataBean.getBank_name());
                    startActivity(i);
                    finish();
                }else{
                    SharedPrefsUtil.putValue(SelectBankActivity.this, "wallet", "bank_name",dataBean.getBank_name());
                    startActivity(new Intent(getApplicationContext(), ModifyBankCardsActivity.class));
                    finish();
                }

            }
        });
    }
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);

    }
    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }

    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }

}
